const tableHead = {
    "geneid": {"title": "Gene ID", "searchType": "text", "formatter": "GetGeneId"},
    "chr": {"title": "Chromsome", "searchType": "text", "formatter": ""},
    "start": {"title": "Strat Position", "searchType": "num", "formatter": ""},
    "end": {"title": "End Position", "searchType": "num", "formatter": ""},
    "strand": {"title": "Strand", "searchType": "text", "formatter": ""},
    "eggnogOgs": {"title": "eggNOG OGs", "searchType": "text", "formatter": ""},
    "narrOgName": {"title": "narr OG name", "searchType": "text", "formatter": ""},
    "narrOgCat": {"title": "narr OG cat", "searchType": "text", "formatter": ""},
    "narrOgDesc": {"title": "narr OG desc", "searchType": "text", "formatter": ""},
    "bestOgName": {"title": "best OG name", "searchType": "text", "formatter": ""},
    "bestOgCat": {"title": "best OG cat", "searchType": "text", "formatter": ""},
    "bestOgDesc": {"title": "best OG desc", "searchType": "text", "formatter": ""},
    "preferredName": {"title": "Preferred name", "searchType": "text", "formatter": ""},
    "gos": {"title": "GOs", "searchType": "text", "formatter": ""},
    "ec": {"title": "EC", "searchType": "text", "formatter": ""},
    "keggKo": {"title": "KEGG ko", "searchType": "text", "formatter": ""},
    "keggPathway": {"title": "KEGG Pathway", "searchType": "text", "formatter": ""},
    "keggModule": {"title": "KEGG Module", "searchType": "text", "formatter": ""},
    "keggReaction": {"title": "KEGG Reaction", "searchType": "text", "formatter": ""},
    "keggRclass": {"title": "KEGG rclass", "searchType": "text", "formatter": ""},
    "brite": {"title": "BRITE", "searchType": "text", "formatter": ""},
    "keggTc": {"title": "KEGG TC", "searchType": "text", "formatter": ""},
    "cazy": {"title": "CAZy", "searchType": "text", "formatter": ""},
    "biggReaction": {"title": "BiGG Reaction", "searchType": "text", "formatter": ""},
    "pfams": {"title": "PFAMs", "searchType": "text", "formatter": ""}
};

const blockHead = {
    "geneid": {"title": "Gene ID", "searchType": "text", "formatter": "GetGeneId"},
    "species": {"title": "Species", "searchType": "text", "formatter": "GetSpecies"},
    "chr": {"title": "Chromsome", "searchType": "text", "formatter": ""},
    "start": {"title": "Strat Position", "searchType": "num", "formatter": ""},
    "end": {"title": "End Position", "searchType": "num", "formatter": ""},
    "strand": {"title": "Strand", "searchType": "text", "formatter": ""}
};

const hideColumns = ["eggnogOgs", "narrOgName", "narrOgCat", "narrOgDesc", "bestOgName", "bestOgCat", "bestOgDesc",
    "preferredName", "gos", "ec", "keggKo", "keggPathway", "keggModule", "keggReaction", "keggRclass", "brite",
    "keggTc", "cazy", "biggReaction", "pfams"
]

function getTableColumns(headJson, table) {
    let columns = [];
    let checkbox = "";
    $.each(headJson, (field, json) => {
        let column = {
            field: field,
            title: json.title,
            sortable: true,
            searchType: json.searchType
        }

        if (json.searchType === "checkbox") {
            column["searchSelect"] = json.searchSelect
        }

        if (json.formatter !== "") {
            column["formatter"] = json.formatter
        }

        columns.push(column)
        checkbox += `<div class="form-check"><label class="form-check-label">
                            <input type='checkbox' checked='checked' value="${field}"  onclick="setColumns('${field}','${table}')"> ${json.title} 
                           <i class="input-helper"></i> </label></div>`
    })
    $(`#${table}-check`).html(checkbox)
    return columns;
}

function GetGeneId(value, row) {
    return `<a href="/alfalfa/browse/gene/${row.id}" target="_blank">${value}</a>`
}

function GetSpecies(value, row) {
    let species = ""
    if(value === "XJDY"){
        species = "M. sativa cultivar XinJiangDaYe"
    }else if(value === "ZMNO"){
        species = "M. sativa cultivar Zhongmu No. 1"
    }else if(value === "MSAE"){
        species = "M. sativa spp. caerulea"
    }else{
        species = "M. truncatula"
    }
    return `<a href="/alfalfa/browse/genome/${value}" target="_blank">${species}</a>`
}

function initTable() {
    const columns = getTableColumns(tableHead, "table")
    $("#table").bootstrapTable({columns: columns})
}